//
//  PBOrderTimeCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/12.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBOrderTimeCell.h"
#import "XZPickView.h"
#import "NSString+TMGetDate.h"

@interface PBOrderTimeCell ()<XZPickViewDelegate,XZPickViewDataSource>

@property (strong, nonatomic) IBOutlet UILabel *chooseTimeLabel;
@property (nonatomic,strong) XZPickView      *pickView;

@property (nonatomic,copy) NSDictionary * dateDic;
@property (nonatomic,copy) NSString * weekStr;
@property (nonatomic,copy) NSString * timeStr;
@property (nonatomic, strong) NSString *selectDate;
@property (nonatomic, assign) NSInteger currentSelectDay;

@end


@implementation PBOrderTimeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseTimeMethod)];
    [self.chooseTimeLabel addGestureRecognizer:tap];
}

- (void)refreshWithString:(NSString *)string {
    self.chooseTimeLabel.text = string;
}

- (void)chooseTimeMethod {
    self.dateDic = [self LHGetStartTime];
    self.weekStr = self.dateDic[@"week"][0];
    self.timeStr = [[self.dateDic[@"time"] objectAtIndex:0] objectAtIndex:0];
    [self.pickView reloadData];
    [[UIApplication sharedApplication].keyWindow addSubview:self.pickView];
    [self.pickView show];
    if (self.delegate && [self.delegate respondsToSelector:@selector(keyboard)]) {
        [self.delegate keyboard];
    }
}


#pragma mark - XZPickViewDelegate, XZPickViewDataSource
-(void)pickView:(XZPickView *)pickerView confirmButtonClick:(UIButton *)button{
    NSInteger left = [pickerView selectedRowInComponent:0];
    NSInteger right = [pickerView selectedRowInComponent:1];
    self.selectDate = [[self.dateDic[@"time"] objectAtIndex:left] objectAtIndex:right];
    self.weekStr = [self.dateDic[@"week"] objectAtIndex:left];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(backWithDate:time:)]) {
        [self.delegate backWithDate:self.weekStr time:self.selectDate];
    }
}

-(NSInteger)pickerView:(XZPickView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    //时间
    if (component == 0) {
        return [self.dateDic[@"week"] count];
    }else{
        NSInteger whichWeek = [pickerView selectedRowInComponent:0];
        return [[self.dateDic[@"time"] objectAtIndex:whichWeek] count];
    }
}

-(void)pickerView:(XZPickView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if(component == 0){
        self.currentSelectDay = [pickerView selectedRowInComponent:0];
        [pickerView pickReloadComponent:1];
        self.weekStr = self.dateDic[@"week"][row];
        NSArray *arr = [[self.dateDic objectForKey:@"time"] objectAtIndex:self.currentSelectDay];
        NSDate *date = [arr objectAtIndex:[pickerView selectedRowInComponent:1]];
        self.timeStr = [self XZGetTimeStringWithDate:date dateFormatStr:@"HH:mm"];
    }else{
        NSInteger whichWeek = [pickerView selectedRowInComponent:0];
        self.timeStr = [[self.dateDic[@"time"] objectAtIndex:whichWeek] objectAtIndex:row];
    }
    
}

-(NSString *)pickerView:(XZPickView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if(component == 0){
        return self.dateDic[@"week"][row];
    }else{
        NSString *str = [[self.dateDic[@"time"] objectAtIndex:self.currentSelectDay] objectAtIndex:row];
        return str;
    }
}

-(NSInteger)numberOfComponentsInPickerView:(XZPickView *)pickerView{
    return 2;
}
- (NSString *)XZGetTimeStringWithDate:(NSDate *)date dateFormatStr:(NSString *)dateFormatStr {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = dateFormatStr;
    return [format stringFromDate:date];
}

- (NSDictionary *)LHGetStartTime {
    // 获取当前date
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDictionary *weekDict = @{@"2" : @"周一", @"3" : @"周二", @"4" : @"周三", @"5" : @"周四", @"6" : @"周五", @"7" : @"周六", @"1" : @"周日"};
    // 日期格式
    NSDateFormatter *fullFormatter = [[NSDateFormatter alloc] init];
    fullFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    // 存放周几和时间的数组
    NSMutableArray *weekStrArr = [NSMutableArray array];
    NSMutableArray *detailTimeArr = [NSMutableArray array];
    // 设置合适的时间
    for (int i = 0; i < 7; i++) {
        NSDate *new = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:date options:NSCalendarMatchStrictly];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM月dd日"];
        NSString *newString = [dateFormatter stringFromDate:new];
        NSInteger week = [calendar component:NSCalendarUnitWeekday fromDate:new];
        // 周几
        NSString *weekStr = weekDict[[NSString stringWithFormat:@"%ld",week]];
        NSString *todayOrOther = @"";
        if (i == 0) {
            todayOrOther = @"今天";
        }else if (i == 1) {
            todayOrOther = @"明天";
        }else if (i == 2){
            todayOrOther = @"后天";
        }else {
            
            todayOrOther = newString;
        }
        // 今天周几 明天周几 后天周几
        NSString *resultWeekStr = [NSString stringWithFormat:@"%@ %@",todayOrOther,weekStr];
        [weekStrArr addObject:resultWeekStr];
        
        // 把符合条件的时间筛选出来
        NSMutableArray *smallArr = [NSMutableArray array];
        [smallArr addObject:@"全天"];
        [smallArr addObject:@"上午"];
        [smallArr addObject:@"下午"];
        [smallArr addObject:@"晚上"];
        [detailTimeArr addObject:smallArr];
    }
    NSDictionary *resultDic = @{@"week" : weekStrArr , @"time" : detailTimeArr};
    return resultDic;
}


#pragma mark - getter methods
-(XZPickView *)pickView{
    if(!_pickView){
        _pickView = [[XZPickView alloc]initWithFrame:[UIScreen mainScreen].bounds title:@"请选择"];
        _pickView.delegate = self;
        _pickView.dataSource = self;
    }
    return _pickView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
